package net.xxcl.kygl.service;

import java.util.List;
import net.xxcl.kygl.mapper.ChangerankMapper;
import net.xxcl.kygl.mapper.UserProjectMapper;
import net.xxcl.kygl.pojo.Changerank;
import net.xxcl.kygl.pojo.Criteria;
import net.xxcl.kygl.pojo.UserProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class ChangerankService {
    @Autowired
    private ChangerankMapper changerankMapper;
    @Autowired
    private UserProjectMapper userProjectMapper;

    private static final Logger logger = LoggerFactory.getLogger(ChangerankService.class);

    public int countByExample(Criteria example) {
        int count = this.changerankMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    public Changerank selectByPrimaryKey(Integer id) {
        return this.changerankMapper.selectByPrimaryKey(id);
    }

    public List<Changerank> selectByExample(Criteria example) {
        return this.changerankMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return this.changerankMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Changerank record) {
        return this.changerankMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Changerank record) {
        return this.changerankMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.changerankMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Changerank record, Criteria example) {
        return this.changerankMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Changerank record, Criteria example) {
        return this.changerankMapper.updateByExample(record, example);
    }

    public int insert(Changerank record) {
        return this.changerankMapper.insert(record);
    }

    public int insertSelective(Changerank record) {
        return this.changerankMapper.insertSelective(record);
    }

    public int change(Changerank changerank,int newState) {
        if(newState==1){
            String[] ids = changerank.getNewids().split(",");
            userProjectMapper.deleteByProjectId(changerank.getProjectid());
            UserProject userProject = new UserProject();
            userProject.setProjectid(changerank.getProjectid());
            for(int i=0;i<ids.length;i++){
                userProject.setRank(i+1);
                userProject.setUserid(Integer.parseInt(ids[i]));
                this.userProjectMapper.insert(userProject);
            }
            changerank.setState(1);
        } else {
            changerank.setState(2);
        }
        return changerankMapper.updateByPrimaryKeySelective(changerank);
    }
}